package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.Applyout;
import com.qst.pojo.User;
import com.qst.pojo.Userlist;
import com.qst.pojo.Zulist;
import com.qst.service.ApplyoutService;
import com.qst.service.UserlistService;
import com.qst.service.ZulistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller @RequestMapping("/applyout") public class ApplyoutController {
	 @Autowired private ZulistService zulistService;
	 @Autowired private ApplyoutService applyoutService;
	 @Autowired private UserlistService userlistService;

	 // 插入退租信息
	 @RequestMapping("/insertapplyout") public String insertapplyout(String house_id, Model model) {
		  Zulist zulist = zulistService.findZuListByHouseId(house_id);
		  applyoutService.insertapplyout(zulist);
		  model.addAttribute("error", "applysuccess");
		  return "redirect:/zulist/myzulist.action";
	 }

	 // 管理员和房东查看退租申请
	 @RequestMapping("/findallapplyout") public String findallapplyout(HttpSession httpSession,
			 Model model,
			 @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
		  PageHelper.startPage(page, pageSize);
		  User user = (User) httpSession.getAttribute("user");
		  List<Applyout> applyout = new ArrayList<Applyout>();
		  if ("admin".equals(user.getUsertype())) {
				applyout = applyoutService.findAllApplyout();
		  } else {
				applyout = applyoutService.findAllApplyout(user.getId());
		  }
		  PageInfo<Applyout> p = new PageInfo<Applyout>(applyout);
		  model.addAttribute("applyout", applyout);
		  model.addAttribute("p", p);
		  model.addAttribute("mainPage", "../applyout/applyout.jsp");
		  return "common/main";
	 }

	 // 管理员拒绝退租申请
	 @RequestMapping("/refuseapplyout") public String refuseapplyout(Model model, Integer id) {
		  Applyout applyout = new Applyout();
		  applyout.setId(id);
		  applyout.setStatus("已拒绝");
		  applyoutService.updateapplyout(applyout);
		  model.addAttribute("mainPage", "applyout.jsp");
		  return "redirect:findallapplyout.action";
	 }

	 // 管理员同意退租申请
	 @RequestMapping("/agreeapplyout") public String agreeapplyout(Model model, Integer id) {
		  applyoutService.agreeapplyout(id);
		  model.addAttribute("error", "applyoutsucess");
		  return "redirect:findallapplyout.action";
	 }

	 // 删除申请退租列表
	 @RequestMapping("/deleteapplyout") public String deleteapplyout(HttpSession httpSession,
			 Model model, Integer id) {
		  applyoutService.deleteapplyout(id);
		  model.addAttribute("error", "deletesucess");
		  User user = (User) httpSession.getAttribute("user");
		  String url = "redirect:getmyapplyout.action";
		  if ("admin".equals(user.getUsertype()) || "fangdong".equals(user.getUsertype())) {
				url = "redirect:findallapplyout.action";
		  }
		  return url;
	 }

	 // 租客查看自己的 退房申请
	 @RequestMapping("/getmyapplyout") public String getmyapplyout(Model model,
			 HttpSession httpSession, @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
		  User user1 = (User) httpSession.getAttribute("user");
		  Userlist userlist = userlistService.findhasuserlist(user1.getId());
		  PageHelper.startPage(page, pageSize);
		  List<Userlist> list = userlistService.getmyapplyout(userlist.getId());
		  PageInfo<Userlist> p = new PageInfo<Userlist>(list);
		  model.addAttribute("userlist", list);
		  model.addAttribute("p", p);
		  model.addAttribute("mainPage", "../applyout/myapplyout.jsp");
		  return "common/main";
	 }
}
